package cn.tedu.medicalpath.prescription.service.impl;

import cn.tedu.medicalpath.base.constant.PrescriptionStatus;
import cn.tedu.medicalpath.medicine.mapper.MedicineMapper;
import cn.tedu.medicalpath.prescription.mapper.PrescriptionMapper;
import cn.tedu.medicalpath.prescription.mapper.PrescriptionMedicineMapper;
import cn.tedu.medicalpath.prescription.pojo.dto.PrescriptionMedicineQuery;
import cn.tedu.medicalpath.prescription.pojo.dto.PrescriptionQuery;
import cn.tedu.medicalpath.prescription.pojo.dto.PrescriptionSaveParam;
import cn.tedu.medicalpath.prescription.pojo.dto.PrescriptionUpdateParam;
import cn.tedu.medicalpath.prescription.pojo.entity.Prescription;
import cn.tedu.medicalpath.prescription.pojo.vo.PrescriptionMedicineVO;
import cn.tedu.medicalpath.prescription.pojo.vo.PrescriptionVO;
import cn.tedu.medicalpath.prescription.service.PrescriptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    PrescriptionMapper prescriptionMapper;
    @Autowired
    PrescriptionMedicineMapper prescriptionMedicineMapper;

    @Autowired
    private MedicineMapper medicineMapper;

    // 保存处方单
    @Override
    public void savePrescription(PrescriptionSaveParam prescriptionSaveParam) {
        log.debug("【保存处方单】业务-入参：{}",prescriptionSaveParam);
        Prescription prescription = new Prescription();
        BeanUtils.copyProperties(prescriptionSaveParam,prescription);
        if (prescription.getId()==null){
            //新增处方单
            prescription.setStatus(PrescriptionStatus.UNSAVED);
            prescription.setCreateTime(new Date());
            log.debug("新增中-【保存处方单】业务-入参：{}",prescription);
            prescriptionMapper.insertPrescription(prescription);
            log.debug("新增成功-【保存处方单】业务-入参,出参：{}",prescription);
        }else {
            //修改处方单
            prescription.setUpdateTime(new Date());
            log.debug("修改中-【保存处方单】业务-,入参：{}",prescription);
            prescriptionMapper.savePrescription(prescription);
            log.debug("修改成功-【保存处方单】业务-入参：{}",prescription);
        }
    }

    //查询处方单
    @Override
    public List<PrescriptionVO> selectPrescription(PrescriptionQuery prescriptionQuery) {
        log.debug("【查询处方单】业务-入参：{}",prescriptionQuery);
        List<PrescriptionVO> list=prescriptionMapper.selectPrescription(prescriptionQuery);
        log.debug("查询结果-【查询处方单】业务-入参：{},出参：{}",prescriptionQuery,list);
        return list;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updatePrescriptionById(PrescriptionUpdateParam prescriptionUpdateParam) {
        log.debug("【修改处方单】业务-入参：{}",prescriptionUpdateParam);
        Prescription prescription = new Prescription();
        BeanUtils.copyProperties(prescriptionUpdateParam,prescription);
        prescription.setUpdateTime(new Date());
        prescriptionMapper.updatePrescriptionById(prescription);
        if (PrescriptionStatus.PICKED.equals(prescriptionUpdateParam.getStatus())){
            updateMedicineAmount(prescriptionUpdateParam.getId());
        }
    }

    @Override
    public long insertNull(Long patientId, Long doctorId) {
        log.debug("【新增空处方单】业务-入参：{},{}",patientId,doctorId);
        Prescription prescription = new Prescription();
        prescription.setPatientId(patientId);
        prescription.setDoctorId(doctorId);
        prescription.setStatus(PrescriptionStatus.UNSAVED);
        prescription.setCreateTime(new Date());
        prescriptionMapper.insertNullPrescription(prescription);
        log.debug("【新增空处方单】业务-入参：{},{}，出参：{}",patientId,doctorId,prescription.getId());
        return prescription.getId();
    }

    public void updateMedicineAmount(Long id) {
        PrescriptionMedicineQuery prescriptionMedicineQuery
                = new PrescriptionMedicineQuery();
        prescriptionMedicineQuery.setPrescriptionId(id);
        List<PrescriptionMedicineVO> prescriptionMedicineVOS
                = prescriptionMedicineMapper.selectPrescriptionMedicine(prescriptionMedicineQuery);
        prescriptionMedicineVOS.forEach(medicineVO ->
            medicineMapper.updateMedicineAmount(medicineVO.getMedicineId(),medicineVO.getNumber())
        );
    }

    /*@Override
    public void updatePrescriptionPriceByPrescriptionId(Long id) {
        log.debug("【更新处方单】业务-入参-处方单id：{}",id);
        //查询所有的处方单药品 累加存入处方单
        Integer price = 0;
        Integer insurancePrice = 0;
        //补充更新处方单时间
        Prescription prescription = new Prescription();
        prescription.setId(id);
        prescription.setUpdateTime(new Date());
        //查询所有的处方单药品
        PrescriptionMedicineQuery prescriptionMedicineQuery = new PrescriptionMedicineQuery();
        prescriptionMedicineQuery.setPrescriptionId(id);
        List<PrescriptionMedicineVO> prescriptionMedicineVOS = prescriptionMedicineMapper.selectPrescriptionMedicine(prescriptionMedicineQuery);
        if (prescriptionMedicineVOS.isEmpty()){
            log.debug("【更新处方单】业务-入参-处方单id：{}，没有处方单药品",id);
            prescription.setPrice(price);
            prescription.setInsurancePrice(insurancePrice);
            prescriptionMapper.savePrescription(prescription);
            return;
        }
        for (PrescriptionMedicineVO prescriptionMedicineVO : prescriptionMedicineVOS) {
            //遍历 所有的处方单 累加价格
            log.debug("【更新处方单】业务-入参-处方单id：{}，处方单药品：{}",id,prescriptionMedicineVO);
            price += prescriptionMedicineVO.getPrice();
            insurancePrice += prescriptionMedicineVO.getInsurancePrice();
        }
        prescription.setPrice(price);
        prescription.setInsurancePrice(insurancePrice);
        log.debug("【更新处方单】业务-入参-处方单id：{}，价格：{}，医保结算金额：{}",id,price,insurancePrice);
        prescriptionMapper.savePrescription(prescription);
    }*/
}
